package struts.action.etudiant;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import metier.Etudiant;
import metier.Stage;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import dao.DAOCandidature;
import dao.DAODepartement;
import dao.DAOEntreprise;
import dao.DAOEtudiant;
import dao.DAOStage;

public class ActionListeStageEtu extends Action {
	
	private DAOStage daoStage=null;
	private DAOCandidature daoCandidature=null;
	private DAOEtudiant daoEtud=null;
	private DAOEntreprise daoEntreprise=null;
	private DAODepartement daoDepartement=null;
	
	public DAODepartement getDaoDepartement() {
		return daoDepartement;
	}

	public void setDaoDepartement(DAODepartement daoDepartement) {
		this.daoDepartement = daoDepartement;
	}

	public DAOStage getDaoStage() {
		return daoStage;
	}
	
	public void setDaoStage(DAOStage daoStage) {
		this.daoStage = daoStage;
	}
	
	public DAOCandidature getDaoCandidature() {
		return daoCandidature;
	}
	
	public void setDaoCandidature(DAOCandidature daoCandidature) {
		this.daoCandidature = daoCandidature;
	}
	
	public DAOEtudiant getDaoEtud() {
		return daoEtud;
	}
	
	public void setDaoEtud(DAOEtudiant daoEtud) {
		this.daoEtud = daoEtud;
	}
	
	public DAOEntreprise getDaoEntreprise() {
		return daoEntreprise;
	}
	
	public void setDaoEntreprise(DAOEntreprise daoEntreprise) {
		this.daoEntreprise = daoEntreprise;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception
	{		
		HttpSession session = request.getSession(false);
		
		Etudiant etud = (Etudiant) session.getAttribute("etudiant");
		
		if(etud == null)
			return mapping.findForward("fatalError");
		
		ArrayList<Stage> liste = getDaoStage().loadAllValides(etud.getNumDep());
		ArrayList<String> entreprises = new ArrayList<String>();
		ArrayList<String> departement = new ArrayList<String>();
		
		
		if(liste.size() == 0)
		{
			request.setAttribute("NOTHING", "Il n'y a aucun stage de disponible.");
			return mapping.findForward("affichStagesEtu");
		}
		
		for(Stage s : liste)
		{
			String nomEnt = getDaoEntreprise().get(s.getReferenceEntreprise()).getNom();
			entreprises.add(nomEnt);
			
			String nomDep = getDaoDepartement().get(s.getReferenceDepartement()).getNom();
			departement.add(nomDep);			
		}
		
		request.setAttribute("STAGES", liste);
		request.setAttribute("ENTREPRISES", entreprises);
		request.setAttribute("DEPARTEMENTS", departement);
		
		return mapping.findForward("affichStagesEtu");
	}
}
